package com.example.dto.base;

import lombok.Data;
import org.springframework.web.bind.annotation.ControllerAdvice;

import java.io.Serializable;

/**
 * @Classname Result
 * @Description TODO 统一返回结果集
 * @Version 1.0.0
 * @Date 2023/1/26 12:23
 * @Created by wlh12
 */
@Data
@ControllerAdvice
public class Result<T> implements Serializable {

    private String code = "200";
    private Boolean status = true;
    private T data;
    private String message = "成功";

    public static Result<Void> success(){
        Result<Void> result = new Result();
        result.setCode("200");
        result.setData(null);
        result.setStatus(true);
        result.setMessage("成功");
        return result;
    }

    public static Result<Void> success(String message){
        Result<Void> result = new Result();
        result.setCode("200");
        result.setData(null);
        result.setStatus(true);
        result.setMessage(message);
        return result;
    }

    public static<T> Result<T> successData(T data){
        Result<T> result = new Result();
        result.setCode("200");
        result.setData(data);
        result.setStatus(true);
        result.setMessage("成功");
        return result;
    }

    public static<T> Result<T> successData(T data,String message){
        Result<T> result = new Result();
        result.setCode("200");
        result.setStatus(true);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    public static Result<Void> fail(){
        Result<Void> result = new Result();
        result.setCode("fail");
        result.setStatus(false);
        result.setData(null);
        result.setMessage("失败");
        return result;
    }

    public static Result<Void> fail(String message ,String code){
        Result<Void> result = new Result();
        result.setCode(code);
        result.setData(null);
        result.setStatus(false);
        result.setMessage(message);
        return result;
    }

    public static Result<Void> fail(String message){
        Result<Void> result = new Result();
        result.setCode("fail");
        result.setData(null);
        result.setStatus(false);
        result.setMessage(message);
        return result;
    }
}
